﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Ylh.FactoryDAL;

namespace Ylh.BLL
{
    public class StaffBLL
    {
        private string ConStr = string.Empty;
        public StaffBLL(string ConStr)
        {
            this.ConStr = ConStr;
        }

        public DataTable IsLogin(string LoginName, string Pwd)
        {
            Ylh.IDAL.IStaff Istaff = DataAccess.CreateIStaff();
            return Istaff.IsLogin(ConStr, LoginName, Pwd);

        }



        public void improtDataBase(DataTable dt, string departmentID)
        {
            Ylh.IDAL.IStaff Istaff = DataAccess.CreateIStaff();

            Ylh.IDAL.IStaffInfo Istaffinfo = DataAccess.CreateIStaffInfo();

            Ylh.Model.TbStaff staff;
            Ylh.Model.TbStaffInfo staffinfo;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];

                staff = new Model.TbStaff();
                staff.DepartmentID = Convert.ToInt32(departmentID);
                staff.IsDeparture = false;
                staff.RoleID = null;
                staff.StaffLoginName = dr["姓名"].ToString();
                staff.StaffPwd = "111111";
                staff.StaffName = dr["姓名"].ToString();
                staffinfo = new Model.TbStaffInfo();
                staffinfo.StaffID = Istaff.InsertInStaff(ConStr, staff);
                staffinfo.Sex = dr["性别"].ToString();
                staffinfo.TelPhone = dr["手机号码"].ToString();
                staffinfo.Marital = dr["婚姻状况"].ToString();
                staffinfo.Hometown = dr["籍贯"].ToString();
                staffinfo.IDCard = dr["身份证号码"].ToString();
                if (dr["出生年月日"] != null && !string.IsNullOrEmpty(dr["出生年月日"].ToString()))
                {
                    staffinfo.Birdthday = Convert.ToDateTime(dr["出生年月日"].ToString());
                }
                staffinfo.Address = dr["现在居住地址"].ToString();
                staffinfo.HomeAddress = dr["家庭住址"].ToString();
                staffinfo.mail = dr["邮箱"].ToString();
                staffinfo.QQ = dr["QQ"].ToString();

                Istaffinfo.InsertIntoStaffInfo(ConStr, staffinfo);
            }


        }

        public DataTable GetStaffData(string DepartmentID)
        {
            Ylh.IDAL.IStaff Istaff = DataAccess.CreateIStaff();
            return Istaff.GetStaffData(ConStr, DepartmentID);
        }

        /// <summary>
        /// 根据员工编号删除员工信息
        /// </summary>
        /// <param name="StaffID"></param>
        /// <returns></returns>
        public bool Delete(int StaffID)
        {
            Ylh.IDAL.IStaff Istaff = DataAccess.CreateIStaff();
            return Istaff.Delete(ConStr, StaffID);
        }

        /// <summary>
        /// 删除选中的员工,但是员工拥有客户的话则不能删除
        /// </summary>
        /// <param name="li"></param>
        public void Delete(List<string> li)
        {
            foreach (string str in li)
            {
                if (hasCust(Convert.ToInt32(str)))
                {
                    continue;
                }
                Delete(Convert.ToInt32(str));
            }
        }

        /// <summary>
        /// 判断员工下面是否有客户
        /// </summary>
        /// <param name="StaffID"></param>
        /// <returns></returns>
        public bool hasCust(int StaffID)
        {
            Ylh.IDAL.IStaff Istaff = DataAccess.CreateIStaff();
            return Istaff.hasCust(ConStr, StaffID);
        }


        /// <summary>
        /// 根据员工编号获得员工信息
        /// </summary>
        /// <param name="StaffID"></param>
        /// <returns></returns>
        public DataTable GetStaff(int StaffID)
        {
            Ylh.IDAL.IStaff Istaff = DataAccess.CreateIStaff();
            return Istaff.GetStaff(ConStr, StaffID);
        }




        /// <summary>
        /// 更新员工的角色
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="StaffID">员工编号</param>
        /// <param name="RoleID">角色编号</param>
        /// <returns></returns>
        public bool UpdateStaffRole(int StaffID, string RoleID)
        {
            Ylh.IDAL.IStaff Istaff = DataAccess.CreateIStaff();
            return Istaff.UpdateStaffRole(ConStr, StaffID, RoleID);
        }


        /// <summary>
        /// 更新员工是否离职
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="StaffID">员工编号</param>
        /// <param name="IsDeparture">是否离职</param>
        /// <returns></returns>
        public bool UpdateStaffDeparture(int StaffID, bool IsDeparture)
        {
            Ylh.IDAL.IStaff Istaff = DataAccess.CreateIStaff();
            return Istaff.UpdateStaffDeparture(ConStr, StaffID, IsDeparture);
        }

        /// <summary>
        /// 只能更新员工名称
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="StaffName">员工新名称</param>
        /// <param name="staffID"></param>
        /// <returns></returns>
        public bool UpdateStaff(string StaffName, string staffID, string DepartmentID)
        {

            Ylh.IDAL.IStaff Istaff = DataAccess.CreateIStaff();
            return Istaff.UpdateStaff(ConStr, StaffName, staffID, DepartmentID);
        }

        /// <summary>
        /// 更新员工密码
        /// </summary>
        /// <param name="StaffID">员工ID</param>
        /// <param name="oldPwd">旧密码</param>
        /// <param name="newPwd">新密码</param>
        /// <returns></returns>
        public int UpdateStaffPwd(string ConStr, int StaffID, string oldPwd, string newPwd)
        {
            Ylh.IDAL.IStaff Istaff = DataAccess.CreateIStaff();
            return Istaff.UpdateStaffPwd(ConStr, StaffID, oldPwd, newPwd);
        }

        public string InsertSinle(Model.TbStaffInfo info, string StaffName, string Department)
        {

            Ylh.IDAL.IStaff Istaff = DataAccess.CreateIStaff();
            Ylh.IDAL.IStaffInfo Istaffinfo = DataAccess.CreateIStaffInfo();
            Ylh.Model.TbStaff staff;
            staff = new Model.TbStaff();
            staff.DepartmentID = Convert.ToInt32(Department);
            staff.IsDeparture = false;
            staff.RoleID = null;
            staff.StaffLoginName = StaffName;
            staff.StaffPwd = "111111";
            staff.StaffName = StaffName;
            info.StaffID = Istaff.InsertInStaff(ConStr, staff);
            Istaffinfo.InsertIntoStaffInfo1(ConStr, info);

            return info.StaffID.ToString(); ;
        }



        public int UpdateStaffPwd(int StaffID, string newPwd)
        {
            Ylh.IDAL.IStaff Istaff = DataAccess.CreateIStaff();
            return Istaff.UpdateStaffPwd(ConStr,StaffID,newPwd);
        }


    }
}
